﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using System.Text;
using ExpertMIS.Brokers;

namespace ExpertMIS.ExternalInterface
{
    public partial class LastQuestionList : System.Web.UI.Page
    {
        private QuestionBroker _questionBroker = new QuestionBroker();
        private DomainBroker _domainBroker = new DomainBroker();

        private void RefreshNewQuestions()
        {
            List<QuestionEntity> questions = new List<QuestionEntity>();
            foreach (DomainEntity domain in _domainBroker.GetAllDomains())
            {
                questions.AddRange(_questionBroker.GetDomainLastQuestion(domain.OID, 1));
            }
            repNewQuestions.DataSource = questions;
            repNewQuestions.DataBind();
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshNewQuestions();
        }

        protected void repNewQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                QuestionEntity question = e.Item.DataItem as QuestionEntity;
                Label lblDomains = e.Item.FindControl("lblDomains") as Label;
                StringBuilder sbDomains = new StringBuilder();
                if (question != null && lblDomains != null)
                {
                    foreach (DomainEntity domain in _domainBroker.GetDomainsByQuerytionOID(question.OID))
                    {
                        sbDomains.Append(string.Format("【<a href='../Forums/DomainIntroduce.aspx?oid={0}' target='_blank'>{1}</a>】", domain.OID, domain.DomainName));
                    }

                    lblDomains.Text = sbDomains.ToString();
                    if (question.FAQ)
                    {
                        lblDomains.CssClass = "faq";
                    }
                }
            }
        }
    }
}
